package com.office.personalOffice.contacts.controller;

import com.office.personalOffice.contacts.pojo.TbDept;
import com.office.personalOffice.contacts.pojo.TbStaff;
import com.office.personalOffice.contacts.service.TbDeptService;
import com.office.personalOffice.contacts.service.TbStaffService;
import com.office.util.ExcelUtilXlsx;
import com.office.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 通讯录专用
 * @Auther: 许月
 * @Date: 2022/11/01/16:27
 */
@RestController
@RequestMapping("/tbStaff")
public class TbStaffController {
    @Autowired
    private TbStaffService tbStaffService;

    @Autowired
    private TbDeptService tbDeptService;

    /**
     * 得到员工列表
     * @param tbStaff
     * @param pageModel
     * @return
     */
    @RequestMapping("/list")
    public PageModel<TbStaff> getList(TbStaff tbStaff, PageModel<TbStaff> pageModel){
//        System.out.println("*********************************************************************");
//        System.out.println(tbStaff);
        pageModel = tbStaffService.getList(tbStaff, pageModel);
        return pageModel;
    }

    /**
     * 得到父部门列表列表
     * @return
     */
    @RequestMapping("/getFdept")
    public List<TbDept> getFdept(){
        return tbDeptService.getFirstDeptList();
    }


    /**
     * 通讯录导出
     * @param tbStaff
     * @param pageModel
     * @param response
     * @return
     */
    @RequestMapping("/download")
    public String excelData(TbStaff tbStaff, PageModel<TbStaff> pageModel, HttpServletResponse response){
        pageModel = tbStaffService.getList(tbStaff, pageModel);
        List<Object[]> tbStaffExcelList = new ArrayList<>();
        for(TbStaff staff:pageModel.getData()){
            Object[] exceldata = new Object[4];
            exceldata[0] = staff.getStaffId();
            exceldata[1] = staff.getName();
            exceldata[2] = staff.getDeptName();
            exceldata[3] = staff.getTel();
            tbStaffExcelList.add(exceldata);
        }
        ExcelUtilXlsx excelUtilXlsx = new ExcelUtilXlsx();
        String title = "通讯录";
        String[] colHead = {"员工编号","姓名","部门名称","电话号码"};
        excelUtilXlsx.exportExcel(response,title,colHead,tbStaffExcelList);
        return null;
    }


}
